package com.tianbo.elfii.client.common;

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.image.*;
import javax.imageio.*;

import com.tianbo.elfii.client.common.ElfiiCommonUtil.FeedBackType;

/**  <pre>
     <b>�๦��������</b><pre>
     ��� About ����Ҫ��
     </pre>
     @version 1.0.1
     @author ��ΰ 
*/

public class ElfiiCommonAbout {

    JButton okButton;
    AboutPanel aboutPanel;
    JPanel itemPanel;
    JPanel slidePanel;

    String[] textStrings;

    int infoBgStartX0;
    int infoBgStartY0;

    int tabButtonElfiiX0;
    int tabButtonElfiiY0;
    int tabButtonTutorialsX0;
    int tabButtonTutorialsY0;
    int tabButtonWidth;
    int tabButtonHeight;
    boolean isTabButtonElfiiSelected=true;
    boolean isTabButtonTutorials=false;

    int textSelectedNumber;
    int textStartX;
    int textStartY;
    int textStartMaxY;
    int textStartMinY;
    int textLineHeight;

    int clipBgStartX0;
    int clipBgStartY0;
    int slideBgStartX0;
    int slideBgStartY0;
    int slideButtonX;
    int slideButtonY;
    int slideButtonMinY;
    int slideButtonMaxY;

    JFrame mainFrame;
    ElfiiCommonRunInfo runInfo;
    public void setRunInfo(ElfiiCommonRunInfo runInfo){
        this.runInfo=runInfo;
        this.mainFrame=runInfo.getMainFrame();
    }

    public void dealBackButton(){
        mainFrame.getContentPane().removeAll();
        ElfiiCommonWelcome welcome = new ElfiiCommonWelcome();

        welcome.setRunInfo(runInfo);

        JPanel p=welcome.createViewPanel();
        mainFrame.getContentPane().setLayout(new BorderLayout());
        mainFrame.getContentPane().add(p,BorderLayout.CENTER);
        mainFrame.validate();
    }

    class AboutPanel extends JPanel {

        BufferedImage unselectedImage;
        BufferedImage selectedImage;
        BufferedImage slideBackgroundImage;
        BufferedImage aboutBackgroundImage;

        BufferedImage textViewportImage;
        BufferedImage textViewportBgImage;

        BufferedImage unSelectedAboutTabImage;
        BufferedImage selectedAboutTabImage;

        BufferedImage unSelectedTutorialsTabImage;
        BufferedImage selectedTutorialsTabImage;

        public AboutPanel() {
            File f=null;
            try {
                f=new File("resource-common/images/gw/gw_about_slide_bg.jpg");
                slideBackgroundImage=ImageIO.read(f);
                f=new File("resource-common/images/about/aboutElfiiTextViewport.png");
                textViewportImage=ImageIO.read(f);
                f=new File("resource-common/images/about/aboutElfiiViewportBGPanel.png");
                textViewportBgImage=ImageIO.read(f);
                f=new File("resource-common/images/about/unSelectedAboutElfiiTab.png");
                unSelectedAboutTabImage=ImageIO.read(f);
                f=new File("resource-common/images/about/selectedAboutElfiiTab.png");
                selectedAboutTabImage=ImageIO.read(f);
                f=new File("resource-common/images/about/unSelectedTutorialsTab.png");
                unSelectedTutorialsTabImage=ImageIO.read(f);
                f=new File("resource-common/images/about/selectedTutorialsTab.png");
                selectedTutorialsTabImage=ImageIO.read(f);

                String tmpStr ;
                java.util.Vector <String>  strV=new java.util.Vector <String> ();
                FileReader fr = new FileReader("resource-common/ElfiiAbout.txt");
                BufferedReader br=new BufferedReader(fr);
                while ((tmpStr=br.readLine())!=null)
                    strV.add(tmpStr);
                fr.close();
                textStrings=new String[strV.size()];
                strV.copyInto(textStrings);

            } catch(Exception e) {System.out.print(e+"   "+f);}

            infoBgStartX0=95;
            infoBgStartY0=28;

            tabButtonElfiiX0=infoBgStartX0+5;
            tabButtonElfiiY0=infoBgStartY0+5;
            tabButtonTutorialsX0=infoBgStartX0+310;
            tabButtonTutorialsY0=infoBgStartY0+5;
            tabButtonWidth=290;
            tabButtonHeight=60;

            slideBgStartX0=infoBgStartX0+740;
            slideBgStartY0=infoBgStartY0+59;
            slideButtonMinY=slideBgStartY0+114;
            slideButtonMaxY=slideBgStartY0+350;
            slideButtonX=slideBgStartX0+25;
            slideButtonY=slideButtonMinY;

            clipBgStartX0=infoBgStartX0+20;
            clipBgStartY0=infoBgStartY0+110;

            textSelectedNumber=0;

            textLineHeight=40;
            textStartMaxY=clipBgStartY0+10;

            //- textStrings.length-9,���е�9��clip�Ѿ���ʾ�����ݣ����µ����ݲ�����Ҫ�����Ƶġ�
            //- +20ֻ��Ϊ�˶�����Щ�ռ���ѡ�
            textStartMinY=0-(textStrings.length-9)*textLineHeight+textStartMaxY+20; 
            textStartX=clipBgStartX0;
            textStartY=textStartMaxY; 
        }
    
        public void paintComponent(Graphics g) {
            super.paintComponent(g);

            Graphics2D g2 = (Graphics2D) g ;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
            g2.setColor(Color.black);
            g2.fillRect(0,0,1020,540);
            if (isTabButtonElfiiSelected==true) {
                g2.drawImage(unSelectedTutorialsTabImage,null,infoBgStartX0+303,infoBgStartY0);
                g2.setColor(Color.white);
                g2.fillRect(infoBgStartX0,infoBgStartY0+63,740,380);
                g2.drawImage(selectedAboutTabImage,null,infoBgStartX0-4,infoBgStartY0);
            }else{
                g2.drawImage(selectedTutorialsTabImage,null,infoBgStartX0+302,infoBgStartY0);
                g2.setColor(Color.white);
                g2.fillRect(infoBgStartX0,infoBgStartY0+63,740,380);
                g2.drawImage(unSelectedAboutTabImage,null,infoBgStartX0-1,infoBgStartY0);
            }

            g2.setStroke(new BasicStroke(8.0f));
            //g2.drawRoundRect(infoBgStartX0+3,infoBgStartY0+75,710,435,70,70);

            //g2.setColor(Color.gray);
            //g2.fillRoundRect(infoBgStartX0+8,infoBgStartY0+71,700,435,70,70);

            g2.drawImage(textViewportImage,null,infoBgStartX0+2,infoBgStartY0+71);
            g2.drawImage(slideBackgroundImage,null,slideBgStartX0,slideBgStartY0);
            g2.setStroke(new BasicStroke(7.0f));
            g2.setColor(Color.white);
            g2.fillOval(slideButtonX,slideButtonY-25,44,44);

            //-�������ݿ��Է���ز鿴ÿ����ť������Ƿ���ȷ;
            //testXY(g2);

            if (isTabButtonElfiiSelected==true) {
                g2.setClip(clipBgStartX0,clipBgStartY0,730,350);
                g2.setFont(new Font(ElfiiCommonConstant.Font_Name,Font.BOLD,24));
                for (int i=0; i<textStrings.length; i++) {
                    g2.setColor(Color.white);
                    g2.drawString(textStrings[i],textStartX,textStartY+textLineHeight*i+20);  //+30����Ϊ�����Ǵ������ϻ���
                }
                //-�������ݿ��Է���ز鿴ÿ����ť������Ƿ���ȷ;
                //testXY(g2);
            }

        }
        public Dimension getPreferredSize() {
            return (new Dimension(832,537));
        }
        public void testXY(Graphics2D g2){
            g2.setColor(Color.green);
            g2.setFont(new Font(ElfiiCommonConstant.Font_Name,Font.BOLD,20));
            g2.fillRect(tabButtonElfiiX0,tabButtonElfiiY0,tabButtonWidth,tabButtonHeight);
            g2.fillRect(tabButtonTutorialsX0,tabButtonTutorialsY0,tabButtonWidth,tabButtonHeight);
        }

    }

    public void createAboutPanel(){

        aboutPanel=new AboutPanel();

        aboutPanel.setLayout(new BorderLayout());
        aboutPanel.setBackground(Color.black);
        aboutPanel.setFocusable(true);
        aboutPanel.addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent e) {
                    dealMousePressed(e);
                }
            }
            );

        aboutPanel.addMouseMotionListener(new MouseAdapter(){
                public void mouseDragged(MouseEvent e) {
                    dealMouseDragged(e);
                }
            }
            );
    }

    public JPanel createViewPanel(){

        JPanel p = new JPanel(new BorderLayout()) ;

        JButton backButton=new JButton();
        backButton.addActionListener( new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    dealBackButton();
                    ElfiiCommonUtil.playFeedbackAudio(FeedBackType.CLICK);
                }
            });

        JButton homeButton=runInfo.createHomeButton();
        JPanel topPanel=ElfiiCommonUtil.createTopPanel("����ELFII",backButton,homeButton);
        JPanel bottomPanel=ElfiiCommonUtil.createBottomPanelBlank();
        createAboutPanel();
        p.setLayout(null);
        p.setBackground(Color.black);
        p.add(topPanel);
        topPanel.setBounds(0,0,1024,100);
        p.add(bottomPanel);
        bottomPanel.setBounds(0,668,1024,100);
        p.add(aboutPanel);
        aboutPanel.setBounds(0,101,1000,560);
        return p;
    }

    public void dealMouseDragged(MouseEvent e){
        int x=e.getX();
        int y=e.getY();
        if (x>slideButtonX && x<slideButtonX+50) {
            slideButtonY=y;
            if (slideButtonY>slideButtonMaxY)
                slideButtonY=slideButtonMaxY;
            if (slideButtonY<slideButtonMinY)
                slideButtonY=slideButtonMinY;
            textStartY=(int) (textStartMaxY-(textStartMaxY -textStartMinY)*(slideButtonY-slideButtonMinY)/(slideButtonMaxY-slideButtonMinY));
            aboutPanel.repaint();
        }

    }

    public void dealMousePressed(MouseEvent e){
        int x=e.getX();
        int y=e.getY();
        System.out.println("clicked x="+x+",y="+y+",   x0="+slideBgStartX0);

        if (x>slideButtonX && x<slideButtonX+50) {
            slideButtonY=y;
            if (slideButtonY>slideButtonMaxY)
                slideButtonY=slideButtonMaxY;
            if (slideButtonY<slideButtonMinY)
                slideButtonY=slideButtonMinY;
            textStartY=(int) (textStartMaxY-(textStartMaxY -textStartMinY)*(slideButtonY-slideButtonMinY)/(slideButtonMaxY-slideButtonMinY));
            //System.out.println("clicked textStartY:"+textStartY);
            aboutPanel.repaint();
        }
        if (x>tabButtonElfiiX0 && x<tabButtonElfiiX0+tabButtonWidth && y>tabButtonElfiiY0 && y<tabButtonElfiiY0+tabButtonHeight) {
            isTabButtonElfiiSelected=true;
            isTabButtonTutorials=false;
            aboutPanel.repaint();
        }
        if (x>tabButtonTutorialsX0 && x<tabButtonTutorialsX0+tabButtonWidth && y>tabButtonTutorialsY0 && y<tabButtonTutorialsY0+tabButtonHeight) {
            isTabButtonElfiiSelected=false;
            isTabButtonTutorials=true;
            aboutPanel.repaint();
        }

    }

    public static void main(String[] args) {
        JFrame mainFrame = new JFrame();
        mainFrame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0); 
                }
            }
            ) ;
        mainFrame.setSize(1024,768);
        mainFrame.setLocation(0,0);
        mainFrame.getContentPane().setLayout(new BorderLayout());
        mainFrame.setUndecorated(true);		  

        ElfiiCommonAbout about = new ElfiiCommonAbout();
        mainFrame.getContentPane().add(about.createViewPanel(),BorderLayout.CENTER);
        mainFrame.setVisible(true);
    }

}
